package de.clapp.musicbox.listAdapter;

import java.util.ArrayList;

import android.content.Context;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import de.clapp.CLAppModel;
import de.clapp.R;
import de.clapp.Settings;
import de.clapp.data.Club;
import de.clapp.data.MusicItem;
import de.clapp.musicbox.MusicModel;
import de.clapp.toolkit.ImageManager;

public class AlbumListAdapter extends ArrayAdapter<MusicItem> implements Filterable, MusicListAdapter
{
    private final LayoutInflater inflater;

    private ArrayList<String> workingList;

    private final ArrayList<String> originalList;

    private MusicFilter filter;

    private MusicModel model;

    public static String SelectedAlbum;

    private ListFragment listFragment;

    public AlbumListAdapter(Context context, ListFragment listFragment)
    {
        super(context, R.layout.music_entry_title);

        this.model = MusicModel.getI();
        this.model.addObserver(this);

        this.listFragment = listFragment;
        this.originalList = this.model.getAlbums();
        this.workingList = new ArrayList<String>(this.model.getAlbums());
        this.filter = new MusicFilter();
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setSelectedAlbum(int index)
    {
        SelectedAlbum = this.workingList.get(index);
    }

    @Override
    public int getCount()
    {
        return this.workingList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.music_entry_interpret, parent, false);

            holder = new ViewHolder();

            holder.title = (TextView) convertView.findViewById(R.id.music_entry_title);
            holder.artist = (TextView) convertView.findViewById(R.id.music_entry_artist);
            holder.image = (ImageView) convertView.findViewById(R.id.music_entry_image);

            convertView.setTag(holder);

        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        // Inhalt aus der Liste eintragen
        String album = this.workingList.get(position);
        holder.title.setText(album);
        holder.artist.setText(this.getAlbums(album));

        String tag = "MusicImages\\" + InterpretListAdapter.SelectedArtist + "-" + album + ".png";
        holder.image.setTag(tag);
        Club club = CLAppModel.getInstance().getCurrentClub();
        ImageManager.getInstance().loadBitmap(tag, holder.image, 50, 50, club.getPort(), club.getIpAddress());
        return convertView;
    }

    private String getAlbums(String album)
    {
        int i = 0;
        StringBuffer songs = new StringBuffer();
        for (String song : this.model.getSongsByAlbum(album))
        {
            i++;
            songs.append(i + ") " + song + ", ");
        }

        return songs.toString().substring(0, songs.length() - 2);
    }

    /**
     * The Class ViewHolder.
     */
    private static class ViewHolder
    {
        /** The logo image view. */
        public ImageView image;

        /** The title text view. */
        public TextView title;

        /** The content text view. */
        public TextView artist;
    }

    @Override
    public Filter getFilter()
    {
        return this.filter;
    }

    private class MusicFilter extends Filter
    {

        @Override
        protected FilterResults performFiltering(CharSequence constraint)
        {
            FilterResults filterResults = new FilterResults();
            ArrayList<String> results = new ArrayList<String>();
            if (constraint != null)
            {
                constraint = constraint.toString().toLowerCase();
                for (String item : originalList)
                {
                    if (item.toLowerCase().contains(constraint))
                    {
                        results.add(item);
                    }
                }
                filterResults.values = results;
            }
            return filterResults;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results)
        {
            workingList = (ArrayList<String>) results.values;
            notifyDataSetChanged();
        }

    }

    @Override
    public void dataChanged()
    {
        this.workingList = new ArrayList<String>(this.model.getAlbums());
        super.notifyDataSetChanged();
        this.listFragment.setListShown(true);
    }
}
